// Drummer
// by Andrea Bianchi
// andreabianchi.alsoplantsfly.com

/*
 Copyright (C) 2009, Andrea Bianchi
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


// Includes
#include "global.h"
#include "raster_graphics.h"
#include "stylusEventManager.h"
#include "networkManager.h"
#include "screenManager.h"
#include "drumManager.h"

// gif to include
#include "palib_top.h"
#include "palib_bottom.h" 
#include "wts_top.h"
#include "wts_bottom.h"

#include <string>

using namespace DS;



// FUNCTION PROTOTYPES
void onStylusPress();
void onStyulusReleased();
void onStylusDragged();
void showLogo(void* topImg, void* bottomImg);


// Function: main()
int main(int argc, char ** argv)
{

	PA_Init();    // Initializes PA_Lib
	PA_InitVBL(); // Initializes a standard VBL
  
  showLogo((void*)wts_top, (void*)wts_bottom);
  showLogo((void*)palib_top, (void*)palib_bottom);

	// Text init
	PA_InitText(UPPER_SCREEN, TEXT_LAYER);
	PA_InitText(LOWER_SCREEN, TEXT_LAYER); 

	// loop until not ready with the connection (if press L and R skip this)
   while (!NetworkManager::getInstance()->initConnection())
   {
      // clean up
      PA_ClearTextBg(UPPER_SCREEN); 
      PA_ClearTextBg(LOWER_SCREEN); 
   }	
	
	
	// Init Screen with images
	ScreenManager::getInstance()->init();
	
	// DBG
	//PA_InitText(UPPER_SCREEN, TEXT_LAYER);
	// DBG

	// Register Events
	StyulusEventManager::getInstance()->registerOnStyulsPressed(&onStylusPress);
	StyulusEventManager::getInstance()->registerOnStyulsReleased(&onStyulusReleased);
	StyulusEventManager::getInstance()->registerOnStyulsDragged(&onStylusDragged);


	// clean up
	PA_ClearTextBg(UPPER_SCREEN); 
	PA_ClearTextBg(LOWER_SCREEN); 

	ScreenManager::getInstance()->setBG(TITLE);


	// Infinite loop to keep the program running
	while (true)
	{
		// Stylus
		StyulusEventManager::getInstance()->handleEvents();
			
		// Draw BG
		ScreenManager::getInstance()->draw();

		// Draw Circles
		DrumManager::getInstance()->draw();
		
		// refresh
		PA_CheckLid();
 		PA_WaitForVBL();
 		
		ScreenManager::getInstance()->finalizeDraw();
	}

	NetworkManager::getInstance()->disconnect();
		
	return 0;
}




void onStylusPress()
{
	DrumManager::getInstance()->press();
}

void onStyulusReleased()
{
	DrumManager::getInstance()->release();
}

void onStylusDragged()
{	
	DrumManager::getInstance()->drag();	
}


void showLogo(void* topImg, void* bottomImg)
{
	PA_Init8bitBg(0, 3); 	// Init a 16 bit Bg on screen 0
	PA_Init8bitBg(1, 3); 	// Init a 8 bit Bg on screen 1	
	
	
	PA_SetBrightness(0, -31); // all black
	PA_SetBrightness(1, -31); // all black	
	
	// Load splash while hidden
	PA_LoadGif(	1, (void*)topImg); // Gif File
	PA_LoadGif(	0, (void*)bottomImg); // Gif File

	s8 i;
	// Transition to normal visible background
	for(i = -31; i <= 0; i++){
		PA_SetBrightness(0, i); 
		PA_SetBrightness(1, i); 
		PA_WaitForVBL();		   
	}   

	s16 time = 180; // 180 frames = 3 seconds
	while(time && (!Pad.Newpress.Anykey) && (!Stylus.Newpress)){ // Display until time over or keys pressed
		time--; // time goes by
		PA_WaitForVBL();
	}		
	
	// Transition to black
	for(i = 0; i > -32; i--){
		PA_SetBrightness(0, i); 
		PA_SetBrightness(1, i); 
		PA_WaitForVBL();		   
	}  	

	// Now that it's all black, clean all that and you're ready to go !
	PA_ResetBgSys();
	PA_SetBrightness(0, 0); // normal
	PA_SetBrightness(1, 0); // normal		
}
